package server

import (
	"log"
)

func (s *Server) onBinMessage(id string, data []byte) {
	conn, ok := s.conns[id]
	if !ok {
		log.Println("conn with id: %s not found", id)
		return
	}

	if conn.binRecv == nil {
		return
	}

	binRecvString := JSONToString(conn.binRecv)

	if connId, ok := s.caches[binRecvString]; ok {
		if connId == PROXY_ID {
			s.proxy.sendBinary(data)
		} else {
			s.pb.sendBinary(connId, data)
		}
		delete(s.caches, binRecvString)
	} else {
		receiverid, found := s.findConn(conn.binRecv, conn.identity)
		if found {
			s.pb.sendBinary(receiverid, data)
		} else {
			log.Println("receiver not found")
		}
	}
	s.conns[id].binRecv = nil
}
